﻿/*
 * Created by SharpDevelop.
 * Author: Taranu Bogdan
 * Date: 5/4/2012
 * Time: 9:37 AM
 * 
 */
using System;
using System.Threading;

namespace SensorClient
{
    /// <summary>
    /// Emulates the Crossbow module
    /// </summary>
    public class SensorEmulator : SensorConsumer
    {

        #region Constructors
        /// <summary>
        /// Creates a new SensorEmulator object
        /// </summary>
        /// <param name="packageInterval">Acquisition period of the package</param>
        public SensorEmulator(int packageInterval)
            : base(packageInterval)
        {
            //This should be empty
        }
        #endregion

        #region Protected methods
        /// <summary>
        /// The method is used by the package generation thread.It will
        /// generate an event every <param name="packageInterval"></param>
        /// seconds of which args contain the package values.
        /// </summary>
        protected override void DoAcquistion()
        {
            int tmp = 0;
            int index = 0;

            while (this.isOn == true)
            {
                //EKG
                for (index = (int)PackageFormat.SensorPositionStart.ECG;
                     index < PackageFormat.ECG_VECTOR_LENGTH
                             * PackageFormat.ECG_ELEMENT_SIZE;
                     index += 2)
                {
                    tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                    package[index] = (byte)(tmp & (byte)0xFF);
                    package[index + 1] = (byte)(tmp >> 8);
                }

                //HTA
                index = (int)PackageFormat.SensorPositionStart.HTA;
                //Primary value
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);
                //Secondary value
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index + 2] = (byte)(tmp & (byte)0xFF);
                package[index + 3] = (byte)(tmp >> 8);

                //Pulse
                index = (int)PackageFormat.SensorPositionStart.PULSE;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);

                //Body temperature
                index = (int)PackageFormat.SensorPositionStart.BODY_TEMPERATURE;
                //Fixed part
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.EIGHT_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                //Decimal parth
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.FOUR_BITS_MAX);
                package[index + 1] = (byte)tmp;

                //Weight
                index = (int)PackageFormat.SensorPositionStart.WEIGHT;
                //Fixed part
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);
                //Decimal part
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.FOUR_BITS_MAX);
                package[index + 2] = (byte)(tmp & (byte)0xFF);

                //Glicemia
                index = (int)PackageFormat.SensorPositionStart.GLICEMY;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);

                //Respiratory frequency
                index = (int)PackageFormat.SensorPositionStart.RESPIRATORY_FREQUENCY;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.EIGHT_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);

                //Ambient temperature
                index = (int)PackageFormat.SensorPositionStart.AMBIENT_TEMPERATURE;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);

                //Humidity sensor
                index = (int)PackageFormat.SensorPositionStart.HUMIDITY;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);

                //Light sensor
                index = (int)PackageFormat.SensorPositionStart.LIGHT;
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.TEN_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);
                package[index + 1] = (byte)(tmp >> 8);

                //RFID, Voltage, Gas
                index = (int)PackageFormat.SensorPositionStart.RFID_VOLTAGE_GAS;
                /*
                How the boolean values are generated:
                7 6 5 4 3   2       1          0
                * * * * * GAS_BIT VOLTAGE_BIT RFID_BIT
                */
                tmp = rGen.Next((int)PackageFormat.BitMaxValues.THREE_BITS_MAX);
                package[index] = (byte)(tmp & (byte)0xFF);

                Thread.Sleep(packageInterval);

                SensorsReceivedArgs args = new SensorsReceivedArgs(package);
                OnSensorsReceived(this, args);
            }
        }
        #endregion

    }
}
